{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 列表详解\n",
    "列表数据结构支持很多方法，列表对象的所有方法如下：\n",
    "* list.append(x)\n",
    "在列表末尾添加一个元素。相当于a[len(a):] = [x]\n",
    "* list.extend(iterable)\n",
    "用可迭代对象的元素扩展列表。相当于a[len(a):] = iterable\n",
    "* list.insert(i,x)\n",
    "在指定位置插入元素。第一个参数是插入元素的索引， 因此，a.insert(0, x)在列表开头插入元素，\n",
    "a.insert(len(a), x)等同于a.append(x)\n",
    "* list.remove(x)\n",
    "从列表中删除第一个值为x的元素。未找到指定元素时，触发ValueError异常。\n",
    "* list.pop([i])\n",
    "删除列表中指定位置的元素，并返回被删除的元素。未指定位置时，a.pop()删除并返回列表的最后一个元素。\n",
    "（方法签名中i两边的方括号表示该参数是可选的，不是要输入方括号。这种表示法常见于Python参考库）。\n",
    "* list.clear()\n",
    "删除列表中所有的元素，相当于del a[:]\n",
    "* list.index(x[, start[, end]])\n",
    "返回列表中第一个值为x的元素的零基索引。未找到指定元素时，触发ValueError异常。\n",
    "可选参数start和end是切片符号，用于将搜素限制为列表的特定子结构。返回的索引是相当于整个序列的开始计算的，而不是start参数。\n",
    "* list.count(x)\n",
    "返回列表中元素x出现的次数\n",
    "* list.sort(*, key=None, reverse=False)\n",
    "排序列表中的元素\n",
    "* list.reverse()\n",
    "翻转列表中的元素\n",
    "* list.copy()\n",
    "返回列表中浅拷贝。相当于a[:]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "2\n",
      "3\n",
      "6\n",
      "['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']\n",
      "['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']\n",
      "['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']\n",
      "pear\n",
      "['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange']\n",
      "grape\n",
      "['apple', 'apple', 'banana', 'banana', 'kiwi', 'orange']\n"
     ]
    }
   ],
   "source": [
    "# 多数列表方法示例\n",
    "fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']\n",
    "print(fruits.count('apple'))\n",
    "print(fruits.count('banana'))\n",
    "print(fruits.index('banana'))\n",
    "print(fruits.index('banana', 4))\n",
    "fruits.reverse()\n",
    "print(fruits)\n",
    "fruits.append('grape')\n",
    "print(fruits)\n",
    "fruits.sort()\n",
    "print(fruits)\n",
    "print(fruits.pop())\n",
    "print(fruits)\n",
    "print(fruits.pop(4))\n",
    "print(fruits)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.用列表实现堆栈"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 4, 5]\n",
      "[3, 4, 5, 6, 7]\n",
      "7\n",
      "[3, 4, 5, 6]\n",
      "6\n",
      "5\n",
      "[3, 4]\n"
     ]
    }
   ],
   "source": [
    "stack = [3, 4, 5]\n",
    "print(stack)\n",
    "# push\n",
    "stack.append(6)\n",
    "stack.append(7)\n",
    "print(stack)\n",
    "# pop\n",
    "print(stack.pop())\n",
    "print(stack)\n",
    "print(stack.pop())\n",
    "print(stack.pop())\n",
    "print(stack)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.用列表实现队列\n",
    "列表作为队列效率很低。\n",
    "在列表末尾添加和删除元素很快，但在列表开头插入或移除元素很慢（所有其他元素都要移动一位）\n",
    "实现队列最好用collections.deque,列如："
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eric\n",
      "John\n",
      "deque(['Michael', 'Terry', 'Graham'])\n"
     ]
    }
   ],
   "source": [
    "from collections import deque\n",
    "\n",
    "queue = deque([\"Eric\", \"John\", \"Michael\"])\n",
    "queue.append(\"Terry\")\n",
    "queue.append(\"Graham\")\n",
    "print(queue.popleft())\n",
    "print(queue.popleft())\n",
    "print(queue)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 列表推导式\n",
    "对序列或可迭代对象中的每个元素应用某种操作，用生成的结果创建新的列表。或用满足特定条件的元素创建子序列。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
     ]
    }
   ],
   "source": [
    "# 创建平方值的列表\n",
    "squares = []\n",
    "for x in range(10):\n",
    "    squares.append(x**2)\n",
    "print(squares)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
     ]
    }
   ],
   "source": [
    "squares = list(map(lambda x: x**2, range(10)))\n",
    "print(squares)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
     ]
    }
   ],
   "source": [
    "squares = [x**2 for x in range(10)]\n",
    "print(squares)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "列表推导式的方括号内包括以下内容：\n",
    "一个表达式，后面为一个for语句。\n",
    "然后，是零个或多个for或if子句。\n",
    "结果是表达式依据for和if子句求值计算而得出一个新列表。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]\n"
     ]
    }
   ],
   "source": [
    "# 将两个列表中不相等的元素组合起来\n",
    "results = [\n",
    "    (x, y)\n",
    "    for x in [1, 2, 3]\n",
    "    for y in [3, 1, 4]\n",
    "    if x != y\n",
    "]\n",
    "print(results)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-8, -4, 0, 4, 8]\n",
      "[0, 2, 4]\n",
      "[4, 2, 0, 2, 4]\n",
      "['banana', 'loganberry', 'passion fruit']\n",
      "[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "# 表达式是元组时，必须加上括号\n",
    "vec = [-4, -2, 0, 2, 4]\n",
    "print([x*2 for x in vec])\n",
    "print([x for x in vec if x >= 0])\n",
    "print([abs(x) for x in vec])\n",
    "freshfruit = ['  banana', '   loganberry', 'passion fruit ']\n",
    "print([weapon.strip() for weapon in freshfruit])\n",
    "print([(x, x**2) for x in range(6)])\n",
    "vec = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
    "print([num for elem in vec for num in elem])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 嵌套的列表推导式\n",
    "列表推导式中的初始表达式可以是任何表达式，甚至可以是另一个列表推导式"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]\n"
     ]
    },
    {
     "data": {
      "text/plain": "[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3X4矩阵\n",
    "matrix = [\n",
    "    [1, 2, 3, 4],\n",
    "    [5, 6, 7, 8],\n",
    "    [9, 10, 11, 12],\n",
    "]\n",
    "# 转置矩阵\n",
    "print([[row[i] for row in matrix] for i in range(4)])\n",
    "list(zip(*matrix))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 4, 5]\n",
      "range(3, 6)\n",
      "range(3, 6)\n"
     ]
    }
   ],
   "source": [
    "# 解包实参列表\n",
    "print(list(range(3, 6)))\n",
    "print(range(3, 6))\n",
    "print(range(*[3, 6]))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}